package mobileann.mafamily.utils;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import mobileann.mafamily.entity.IntegralLevelEntity;
import mobileann.mafamily.entity.MedalEntity;
import mobileann.mafamily.medalaward.MedalGotIndicateActivity;

public class LevelEntityUtils {
	private IntegralLevelEntity LE;
	private static LevelEntityUtils instance;

	public synchronized static LevelEntityUtils getInstance() {
		if (instance == null) {
			instance = new LevelEntityUtils();
		}
		return instance;
	}

	public void setLevelEntity(IntegralLevelEntity LE) {
		this.LE = LE;
	}

	public IntegralLevelEntity getLevelEntity() {
		return LE;
	}

	public void refreshMedalInfo(ArrayList<MedalEntity> medals) {
		this.LE.setMedals(medals);
	}

	public int getMedalNum() {
		if (LE != null) {
			int num = 0;
			for (int i = 0; i < LE.getMedals().size(); i++) {
				if (LE.getMedals().get(i).getMedalNum() != 0) {
					num += LE.getMedals().get(i).getMedalNum();
				}
			}
			return num;
		} else {
			return 0;
		}
	}

	public int getExpProgress() {
		if (LE != null) {
			int nowPoints = LE.getPoints();
			int nextPoints = LE.getNext_points();
			if (nextPoints != 0) {
				int nowPro = nowPoints * 100 / nextPoints;
				return nowPro;
			} else {
				return 0;
			}
		} else {
			return 0;
		}
	}

	public String getCoinStr() {
		if (LE != null) {
			return "金币：" + LE.getCoin();
		} else {
			return "金币：0";
		}
	}

	public String getLevelStr() {
		if (LE != null) {
			return "Lv." + LE.getLevel();
		} else {
			return "Lv.0";
		}
	}

	public String getLevelName() {
		if (LE != null) {
			return LE.getRank();
		} else {
			return "";
		}
	}

	public String getMedalStr() {
		if (LE != null) {
			return "勋章：" + getMedalNum();
		} else {
			return "勋章：0";
		}
	}

	public String getExpStr() {
		if (LE != null) {
			return "经验值" + LE.getPoints() + "/" + LE.getNext_points();
		} else {
			return "经验值：0";
		}
	}

	public void getAwardMedal(Context con) {
		if (LE != null) {
			ArrayList<MedalEntity> medals = LE.getMedals();
			for (MedalEntity entity : medals) {
				if (entity.isHasAward()) {
					Intent it = new Intent(con, MedalGotIndicateActivity.class);
					it.putExtra("got_medal_type", entity.getMedalID() - 1);
					con.startActivity(it);
					break;
				}
			}
		}
	}

	public boolean hasMedalAward() {
		if (LE != null) {
			ArrayList<MedalEntity> medals = LE.getMedals();
			for (MedalEntity entity : medals) {
				if (entity.isHasAward()) {
					return true;
				}
			}
		}
		return false;
	}

	public void setViewVisible(View view) {
		if (hasMedalAward()) {
			view.setVisibility(View.VISIBLE);
		} else {
			view.setVisibility(View.GONE);
		}
	}

	/*
	 * 完成的一次性任务的操作码是否存在
	 */
	public boolean isDisposableCode(int codeTag) {
		boolean isCode = false;
		if (LE != null && LE.getDisposable() != null) {
			List<Integer> disposable = LE.getDisposable();
			if (disposable.size() != 0) {
				for (int i = 0; i < disposable.size(); i++) {
					if (codeTag == disposable.get(i)) {
						isCode = true;
						break;
					} else {
						isCode = false;
					}
				}
			}
		}
		return isCode;
	}
}
